﻿#include "navigationitem.h"
#include <QMouseEvent>

NavigationItem::NavigationItem(const QString& text, const QPixmap& icon, QWidget* parent)
    : QWidget(parent), m_selected(false), m_iconSize(24, 24) // 默认图片大小为 24x24
{
    // 设置布局
    QVBoxLayout* layout = new QVBoxLayout(this);
    layout->setAlignment(Qt::AlignCenter);

    // 添加图标
    m_iconLabel = new QLabel(this);
    m_iconLabel->setPixmap(icon.scaled(m_iconSize, Qt::KeepAspectRatio, Qt::SmoothTransformation)); // 缩放图片
    layout->addWidget(m_iconLabel);

    // 添加文本
    m_textLabel = new QLabel(text, this);
    m_textLabel->setAlignment(Qt::AlignCenter);
    layout->addWidget(m_textLabel);

    setLayout(layout);
}

// 设置选中状态
void NavigationItem::setSelected(bool selected)
{
    m_selected = selected;
    if (m_selected)
    {
        setStyleSheet("background-color: #1e9fff; color: white;"); // 选中时的样式
    }
    else
    {
        setStyleSheet("background-color: transparent; color: black;"); // 未选中时的样式
    }
}

// 设置图片大小
void NavigationItem::setIconSize(const QSize& size)
{
    m_iconSize = size;
    const QPixmap* iconPtr = m_iconLabel->pixmap(); // 获取当前图片的指针
    if (iconPtr) // 检查指针是否有效
    {
        QPixmap icon = *iconPtr; // 解引用指针，获取 QPixmap 对象
        m_iconLabel->setPixmap(icon.scaled(m_iconSize, Qt::KeepAspectRatio, Qt::SmoothTransformation)); // 缩放图片
    }
}

// 鼠标点击事件
void NavigationItem::mousePressEvent(QMouseEvent* event)
{
    if (event->button() == Qt::LeftButton)
    {
        emit clicked(); // 发送点击事件信号
    }
    QWidget::mousePressEvent(event);
}
